package com.liuqi.aiagent.controller;

import com.liuqi.aiagent.service.ModelService;
import com.liuqi.aiagent.transfer.ApiResult;
import com.liuqi.aiagent.transfer.dto.ModelEditDto;
import com.liuqi.aiagent.transfer.dto.ModelSaveDto;
import com.liuqi.aiagent.transfer.model.Result;
import com.liuqi.aiagent.transfer.model.ResultList;
import com.liuqi.aiagent.transfer.vo.ModelVo;
import com.liuqi.aiagent.util.NetUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.liuqi.aiagent.util.StringUtil.hasText;
import static com.liuqi.aiagent.util.StringUtil.notHasText;

/**
 * @author liuqi
 * @date 2025/7/15
 **/
@RestController
@RequestMapping("/model")
@AllArgsConstructor
@Api(tags = "AI模型服务接口")
@Slf4j
public class ModelController {

    private final ModelService modelService;

    @ApiOperation("添加模型")
    @PostMapping("/add")
    public Result save(@RequestBody ModelSaveDto modelSaveDto) {
        if (notHasText(modelSaveDto.getName())) {
            return ApiResult.fail("名称为空");
        }
        if (notHasText(modelSaveDto.getType())) {
            return ApiResult.fail("类型为空");
        }
        if (notHasText(modelSaveDto.getUrl())) {
            return ApiResult.fail("服务地址为空");
        }
        if (!NetUtil.isHttpURL(modelSaveDto.getUrl())) {
            return ApiResult.fail("服务地址非法");
        }
        try {
            if (!NetUtil.telnetHttpURL(modelSaveDto.getUrl())) {
                return ApiResult.fail("服务地址连接失败");
            }
        } catch (Throwable t) {
            log.error("URL连接失败：url=" + modelSaveDto.getUrl(), t);
            return ApiResult.fail("服务地址连接失败");
        }
        if (notHasText(modelSaveDto.getApiKey())) {
            return ApiResult.fail("ApiKey为空");
        }
        if (notHasText(modelSaveDto.getModelName())) {
            return ApiResult.fail("模型名称为空");
        }

        return modelService.save(modelSaveDto);
    }

    @ApiOperation("添加模型")
    @PostMapping("/edit")
    public Result edit(@RequestBody ModelEditDto modelEditDto) {
        if (modelEditDto.getId() == null) {
            return ApiResult.fail("模型ID为空");
        }
        if (notHasText(modelEditDto.getName())) {
            return ApiResult.fail("名称为空");
        }
        if (notHasText(modelEditDto.getUrl())) {
            return ApiResult.fail("服务地址为空");
        }
        if (!NetUtil.isHttpURL(modelEditDto.getUrl())) {
            return ApiResult.fail("服务地址非法");
        }
        try {
            if (!NetUtil.telnetHttpURL(modelEditDto.getUrl())) {
                return ApiResult.fail("服务地址连接失败");
            }
        } catch (Throwable t) {
            log.error("URL连接失败：url=" + modelEditDto.getUrl(), t);
            return ApiResult.fail("服务地址连接失败");
        }
        if (notHasText(modelEditDto.getApiKey())) {
            return ApiResult.fail("ApiKey为空");
        }
        if (notHasText(modelEditDto.getModelName())) {
            return ApiResult.fail("模型名称为空");
        }
        return modelService.edit(modelEditDto);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除模型")
    @ApiImplicitParam(name = "id", value = "模型ID", required = true, dataTypeClass = Integer.class)
    public Result delete(Integer id) {
        if (id == null) {
            return ApiResult.fail("ID为空");
        }

        return modelService.delete(id);
    }

    @ApiOperation("查询所有模型")
    @GetMapping("/findAll")
    @ApiImplicitParam(name = "type", value = "模型类型", dataTypeClass = String.class)
    public ResultList<ModelVo> findAll(String type) {
        if (hasText(type)) {
            return modelService.findAllByType(type);
        }
        return modelService.findAll();
    }

}
